var studentPage={}
studentPage.importAll=function (){
    $('#common-modal').modal('show')
    $('.modal-title').html('导入班级信息')
    $.post('student/studentFileUploadTemplate.html',null,function (view){
        $('.common-modal-body').html(view)
    })
    common.commonModalDialogClearSubmitButtonBind()
    $('#modal-submit-button').click(function (){
        var files = $('#student-select-file').prop('files');//表示input中有一个files的属性 存储这我们的文件
        var data = new FormData();
        data.append('file', files[0]);
        $.ajax({
            url:'student/importAllSubmit',
            data:data,
            type:'POST',
            cache:false,
            processData:false,
            contentType:false,
            dataType:false,
            success:function (result){
                alert(result.replaceAll("|","\n"))
                $('#common-modal').modal('hide')

            }
        })
    })
}
studentPage.changeFileContent=function (){
    var fileContent = $('#student-select-file').val();
    if (fileContent.endsWith('.xls') || fileContent.endsWith('.xlsx')){
        $('#fileContent').html($('#student-select-file').val())
    }else {
        alert('请选择正确的excel文件')
    }
}
studentPage.clear=function (){
    $('#grade').val('')
    $('#major').val('')
    $('#class_no').val('')
}
studentPage.classQuery=function (){
    var param = {}
    param.grade=$('#grade').val()
    param.major=$('#major').val()
    param.class_no=$('#class_no').val()
    if (param.grade=='' && param.major=='' && param.class_no==''){
        alert('请选择想要查询的条件')
        return;
    }
    //如果是exam模块 需要做一些页面的调整 需要传递一些参数进行判定
    if($('#examSymbol').val()=='examSymbol'){
        param.examSymbol = 'examSymbol'
    }
    $.post('student/query.html',param,function (view){
        $('#studentClassContent').html(view)
        if ($('#examSymbol').val()=='examSymbol'){
            studentPage.bindStudentClass()
            studentPage.bindStudentClassSelectAll()
            studentPage.DefaultSelectClassStatus()
        }
    })
}
studentPage.bindStudentClass=function (){
    $('#studentClassContent div #classTable tbody tr th input').click(function (){
        var param={}
        param.examId=$('#exam_hidden_id').val()
        param.studentClass=$(this).val()
        if (this.checked){
            //绑定
            $.post('exam/bindStudentClass',param,function (){
                alert('绑定成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }else {
            //解绑
            $.post('exam/unbindStudentClass',param,function (){
                alert('解绑成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }

    })
}
studentPage.bindStudentClassSelectAll=function (){
    $('#studentClassContent div #classTable thead tr th input').click(function (){
        var examId = $('#exam_hidden_id').val()
        var param = {
            examId:examId
        }
        var bodyContent = $('#studentClassContent div #classTable tbody tr th input:checked').length

        if (this.checked){
            if (bodyContent==0){
                alert('请选择想要绑定的班级')
            }else {
                var classes = ''
                $('#studentClassContent div #classTable tbody tr th input').each(function (i,input){
                    classes+=input.value+','
                })
                param.studentClasses=classes
                param.status='绑定'
                $.post('exam/bindAllAndUnbindAll',param,function (){
                    alert('绑定成功')
                    studentPage.bindStudentClassDefaultShow()
                })
            }
        }else {
                var classes = ''
                $('#studentClassContent div #classTable tbody tr th input').each(function (i,input){
                    classes+=input.value+','
                })
                param.studentClasses=classes
                param.status='解绑'
                $.post('exam/bindAllAndUnbindAll',param,function (){
                    alert('解绑成功')
                    studentPage.bindStudentClassDefaultShow()
                })
        }
    })
}
studentPage.bindStudent=function (){
    $('#student-bottom-content #bottom-student-table #student-content tr td input').click(function (){
        var param = {}
        param.examId=$('#exam_hidden_id').val()
        param.studentClass=$('#student-bottom-content form div input').val()
        if (this.checked){
            var studentNumbers = ''
            $('#student-bottom-content #bottom-student-table #student-content tr td input:checked').each(function (i,input){
                studentNumbers += input.value.split('-')[3]+','
            })
            param.studentNumbers = studentNumbers

            $.post('exam/bindStudent',param,function (){
                alert('绑定成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }else {
            //解绑一个学生
            param.studentNumbers = this.value.split('-')[3]
            $.post('exam/unbindStudent',param,function (){
                alert('解绑成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }
    })
}
studentPage.bindStudentSelectAll=function (){
    $('#student-bottom-content #bottom-student-table thead tr th input').click(function (){
        var param={}
        param.examId=$('#exam_hidden_id').val()
        param.studentClass=$('#student-bottom-content form div input').val()
        if (this.checked){
            $.post('exam/bindStudentClass',param,function (){
                alert('绑定成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }else {
            $.post('exam/unbindStudentClass',param,function (){
                alert('解绑成功')
                studentPage.bindStudentClassDefaultShow()
            })
        }
    })

}
studentPage.DefaultSelectClassStatus=function (){
    $.post('exam/selectCacheClass',{examId:$('#exam_hidden_id').val()},function (resultMap){
      for (var key in resultMap){
          $('#studentClassContent div #classTable tbody tr th input').each(function (i,input){
              if (input.value==key){
                  $(input).prop('checked',true)
              }
          })
      }
    })
}
studentPage.DefaultSelectStudentStatus=function (){
    $.post('exam/selectCacheClass',{examId:$('#exam_hidden_id').val()},function (resultMap){
        var className = $('#student-bottom-content form div input').val()
        x:for (var key in resultMap){
            if (className == key){
                if (resultMap[key]=='All'){
                    $('#student-bottom-content #bottom-student-table thead tr th input').prop('checked',true)
                    $('#student-bottom-content #bottom-student-table tbody tr td input').prop('checked',true)
                }else {
                    var studentNumbers = resultMap[key].split(",")//20200011,20200022,20200033
                    $('#student-bottom-content #bottom-student-table tbody tr td input').each(function (index,input){
                        //input中value为2020-软件-1班-202011001  我们需要的是后面的学号
                        y:for (var i = 0;i<studentNumbers.length;i++){
                            if (input.value.split('-')[3]==studentNumbers[i]){
                                $(input).prop('checked',true)
                                break y;
                            }
                        }
                    })
                }
                break x;
            }
        }
    })
}
studentPage.bindStudentClassDefaultShow=function (){
    $.post('exam/defaultShowBindClass.html',{examId:$('#exam_hidden_id').val()},function (view){
        $('#exam_edit_table').replaceWith(view)
    })
}
studentPage.classInfo=function (classParam){
    $('#bottom-class_no').val(classParam)
    var params = classParam.split('-')
    var studentClass ={
        grade:params[0],
        major:params[1],
        class_no:params[2]
    }
    $.post('student/queryClassStudent.html',studentClass,function (view){
        if($('#examSymbol')!=undefined && $('#examSymbol').val()=='examSymbol'){
            $('#common-modal #modal-dialog .modal-content .common-modal-body #student-bottom-content #bottom-student-table #student-content').html(view)
            //需要删除编辑组
            $('#common-modal #modal-dialog .modal-content .common-modal-body #student-bottom-content #bottom-student-table tr th:eq(5)').remove()
            $('#common-modal #modal-dialog .modal-content .common-modal-body #student-bottom-content #bottom-student-table #student-content tr').each(function (i,tr){
                $(tr).children('td:eq(5)').remove()
            })
            studentPage.bindStudentSelectAll()
            studentPage.bindStudent()
            studentPage.DefaultSelectStudentStatus()
        }else {
            $('#student-content').html(view)
        }


    })
}
studentPage.studentQuery=function (){
    var code = $('#code').val()
    var studentName = $('#studentName').val()
    var className = $('#bottom-class_no').val()

    if (code=='' && studentName == '' &&className ==''){
        alert('请选择想要查询的班级或学生')
        return;
    }
    var params = {
        code:code,
        studentName:studentName,
    }
    if (className!=''){
        var classNames = className.split('-')
        params.grade=classNames[0]
        params.major=classNames[1]
        params.class_no=classNames[2]
    }
    $.post('student/queryClassStudent.html',params,function (view){
        $('#student-content').html(view)
    })
}
studentPage.clearStudentQuery=function (){
    $('#code').val('')
    $('#studentName').val('')
    $('#bottom-class_no').val('')
    $('#student-content').html('')
}
studentPage.addStudent=function (className){
    $.post('student/addStudent.html',{className:className},function (view){
        common.commonModalDialog('添加学生',view,function (){
            var studentParam = {
                code:$('#student-add-form #code').val(),
                sname:$('#student-add-form #sname').val(),
                grade:$('#student-add-form #grade').val(),
                major:$('#student-add-form #major').val(),
                class_no:$('#student-add-form #class_no').val()
            }
            $.post('student/addStudent',studentParam,function (result){
               if (result==false){
                   alert('学号重复或者名字重复')
               }else {
                   alert('添加成功')
                   $('#common-modal').modal('hide')
                   $.post('student/query.html',studentParam,function (view){
                       $('#studentClassContent').html(view)
                       studentPage.classInfo(studentParam.grade+'-'+studentParam.major+'-'+studentParam.class_no)
                   })
               }

            })
        })
    })
}
studentPage.limitQuery=function (page){
    var param = {
        grade:$('#student-top-content .form-inline div #grade').val(),
        major:$('#student-top-content .form-inline div #major').val(),
        class_no:$('#student-top-content .form-inline div #class_no').val(),
        page:page
    }
    //如果是exam模块 需要做一些页面的调整 需要传递一些参数进行判定
    if($('#examSymbol')!=undefined && $('#examSymbol').val()=='examSymbol'){
        param.examSymbol = 'examSymbol'
    }
    $.post('student/limitQuery.html',param,function (view){
        $('#studentClassContent').html(view)
    })
}
studentPage.edit=function (code){
    $.post('student/edit.html',{code:code},function (view){
        common.commonModalDialog('编辑学生',view,function (){
            var studentParam = {
                code:$('#student-add-form #code').val(),
                sname:$('#student-add-form #sname').val(),
                grade:$('#student-add-form #grade').val(),
                major:$('#student-add-form #major').val(),
                class_no:$('#student-add-form #class_no').val()
            }
            $.post('student/editSave',studentParam,function (result){
                if (result){
                    alert('编辑成功')
                    $('#common-modal').modal('hide')
                    $.post('student/query.html',studentParam,function (view){
                        $('#studentClassContent').html(view)
                        studentPage.classInfo(studentParam.grade+'-'+studentParam.major+'-'+studentParam.class_no)
                    })
                    return;
                }
                alert('学号或者名字重复')
            })
        })
    })
}
studentPage.selectAll=function (){
    var checked = $('#classTable thead :checkbox').prop('checked')
    $('#classTable tbody :checkbox').prop('checked',checked)
}
studentPage.deleteClass=function (className){
   var classNames ='';
   if (className=='' || className==undefined){
        var checkbox = $('#classTable tbody :checked')
        if (checkbox.length==0){
            alert('请选择想要删除的班级')
            return;
        }
        checkbox.each(function (e,element){
            classNames = classNames+element.value+'|'
        })
   }else {
       classNames = className;
   }
   $.post('student/deleteClass',{className:classNames},function (){
       alert('删除成功')
       $.post('student/query.html',{grade:$('#grade').val(),major:$('#major').val(),class_no:$('#class_no').val()},function (view){
           $('#studentClassContent').html(view)
       })
   })
}
studentPage.selectAllStudent=function (){
    var prop = $('#bottom-student-table thead :checkbox').prop('checked')
    $('#bottom-student-table tbody :checkbox').prop('checked',prop)
}
studentPage.deleteClassStudent=function (studentInfo){
    var studentInfos = '';
    if (studentInfo=='' || studentInfo==undefined){
        var checkedBox = $('#bottom-student-table tbody :checked')
        if (checkedBox.length==0){
            alert('请选择想要删除的学生')
            return;
        }
        checkedBox.each(function (i,element){
            studentInfos = studentInfos+element.value+','
        })
    }else {
        studentInfos = studentInfo
    }
    $.post('student/deleteClassStudent',{studentInfo:studentInfos},function (){
        alert('删除成功')
        var grade = $('#grade').val()
        var major =$('#major').val()
        var class_no=$('#class_no').val()
        if (grade!='' || major!='' || class_no!=''){
            $.post('student/query.html',{grade:grade,major:major,class_no:class_no},function (view){
                $('#studentClassContent').html(view)
            })
        }
        var classInfo = $('#bottom-class_no').val()
        if (classInfo!=''){
            var params = classInfo.split('-')
            var studentClass ={
                grade:params[0],
                major:params[1],
                class_no:params[2]
            }
            $.post('student/queryClassStudent.html',studentClass,function (view){
                $('#student-content').html(view)
            })
        }
    })

}
studentPage.exportClass=function (condition){
    var classInfos = '';
    if (condition=='多个班级导出'){
        var checkbox = $('#classTable tbody :checked')
        if (checkbox.length==0){
            alert('请选择想要导出的班级')
            return;
        }
        checkbox.each(function (i,element){
            classInfos = classInfos + element.value+','
        })
    }else {
        var classInfo = $('#bottom-class_no').val()
        if (classInfo=='' || classInfo==undefined){
            alert('请选择想要导出的班级')
            return;
        }
        classInfos = classInfo
    }
    window.location.href='student/exportClassStudent?classInfo='+classInfos

}